<?php

namespace Biz\service\advertisement\position\model;

use Biz\service\advertisement\AdAdmin;
use Biz\service\advertisement\AdCacheManagement;
use Biz\service\advertisement\AdConfManagement;
use Biz\service\advertisement\position\dao\BizPositionManagementDao;
use Doraemon\model\Position\PositionBaseInfo;
use Doraemon\service\position\PositionRoute;

class BizPositionManagementModel implements BizPositionManagementDao
{

    public static array $conf = [];
    public static array $formatConf = [];
    public static array $conf_ids = [];

    public static function loadConf()
    {
        if (empty(self::$conf)) {
            //资源位配置
            $list = AdAdmin::positionList();
            self::$conf_ids = array_keys($list);
            self::$conf = $list;
            list($items, $noCacheKeys) = AdCacheManagement::getAllPositionCache(self::$conf_ids);
            $rs = empty($ids) ? [] : PositionBaseInfo::getActiveCount(self::$conf_ids, date('Y-m-d H:i:s'));
            $activeCount = empty($rs) ? [] : array_column($rs, 'count', 'position_id');
            $tabGroup = [];
            foreach (self::$conf as $v) {
                $id = $v['id'];
                $v['super_role'] = explode(',', $v['super_role']);
                $v['op_roles'] = explode(',', $v['op_roles']);
                $cache = $items[$id] ?? [];
                $v['cache'] = in_array($id, $noCacheKeys) ? false : implode(',', array_column($cache, 'id'));
                $v['count'] = $activeCount[$id] ?? '-';
                $v['uri'] = PositionRoute::generateRoute($id);
                $v['drop_version'] = AdConfManagement::formatField($v['drop_version']);
                $tabGroup['_all'][] = $v;
            }
            self::$formatConf = $tabGroup;
        }
    }

    public static function list(array $condition = []): array
    {
        self::loadConf();
        $t = $condition['tab'] ?? [];
        $s = $condition['style'] ?? [];
        $v = $condition['v'] ?? [];
        $n = $condition['name'] ?? '';
        $response = [];
        //条件过滤
        foreach (self::$conf as $c) {
            $tCheck = false;
            $tab = is_scalar($c['tab']) ? [$c['tab']] : $c['tab'];
            if ($tab) {
                foreach ($tab as $tabItem) {
                    $tCheck = ($t && is_array($t) && in_array($tabItem, $t));
                    if ($tCheck) break;
                }
            }
            $tCheck = $tCheck || empty($t);
            $sCheck = ($s && is_array($s) && in_array($c['style_type'], $s)) || empty($s);
            $vCheck = ($v && is_array($v) && in_array($c['v'], $v)) || empty($v);
            $nCheck = strlen($n) && strpos($c['en_name'], $n) !== false || strlen($n) == 0;
            if ($tCheck && $sCheck && $vCheck && $nCheck) {
                $c['uri'] = PositionRoute::generateRoute($c['id']);
                $response[] = $c;
            }
        }


        $sort = array_column($response, 'sort');
        array_multisort($sort, SORT_ASC, $response);

        return $response;
    }


    public static function total(array $condition = []): int
    {
        return count(self::list($condition));
    }

    public static function info($id): array
    {
        $format = [];
        self::loadConf();
        $info = self::$conf[$id];
        if ($info) {
            $format = $info;
            if (!empty($format['alias'])) {
                $alias = is_array($format['alias']) ? $format['alias'] : [$format['alias']];
                $format['en_name'] = array_merge([$format['en_name']], $alias);
            }
            $format['drop_version'] = AdConfManagement::formatField($format['drop_version']);
        }
        return $format;
    }
}